﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jy123.DBUtility;
using Jy123.BLL.Public;

namespace Jy123.BLL.Module
{
    public class Module
    {
        public string CreateModule(Jy123.Model.Public_JType JType, int userId, string name, string description)
        {
            try
            {

                Module_Main BLLModule = new Module_Main();
                Jy123.Model.Module_Main module = new Jy123.Model.Module_Main();
                Public_Level BLLLevel = new Public_Level();
                string strWhere = string.Format("LevelNumber =1 AND FJType={0}", JType.ID);
                List<Jy123.Model.Public_Level> listLevel = BLLLevel.GetModelList(strWhere);
                Jy123.Model.Public_Level level = listLevel[0];
                module.FJType = JType.ID;
                module.FUser = userId;
                module.MStatus = 0;
                int moduleId = BLLModule.Add(module);
                int peakValue = 100;//需要改进
                User_Main BLLUser = new User_Main();
                Model.User_Main user = BLLUser.GetModel(userId);
                State state = new State();
                module.ID = moduleId;
                state.CreateModuleState(user, module, Convert.ToInt32(Public.EnumConfig.StateActionType.CreateModule));
                string strRedirect = string.Empty;
                Public_Consume BLLConsume = new Public_Consume();
                List<Model.Public_Consume> listConsume = BLLConsume.GetModelList("FJType = " + JType.ID.ToString());
                switch (JType.JFather.Value)
                {
                    case 1:
                        Venues_Main BLLVenues = new Venues_Main();
                        Jy123.Model.Venues_Main venues = new Jy123.Model.Venues_Main();

                        if (listConsume.Count > 0)
                        {
                            Model.Public_Consume consume = listConsume[0];
                            peakValue = consume.PeakValue.Value;
                        }
                        venues.ID = moduleId;
                        venues.VName = name;
                        venues.FLevel = level.ID;
                        venues.FJType = JType.ID;
                        venues.FUser = userId;
                        venues.Vitality = peakValue;
                        venues.VStatus = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.VenuesStatus.Empty);
                        venues.Assets = 0;
                        venues.RaceTimes = 0;
                        int vId = BLLVenues.Add(venues);
                        if (JType.FSportType.Value == 13 || JType.FSportType.Value == 14)
                        {
                            //赛车需要预设基础赛道
                            BLL.Public_Annexe BLLAnnexe = new Public_Annexe();
                            BLL.Annexe_Record BLLARecord = new Annexe_Record();
                            List<Model.Public_Annexe> listAnnexe = BLLAnnexe.GetModelList("FAType = 8 AND FJType = " + JType.ID.ToString());
                            if (listAnnexe.Count > 0)
                            {
                                Model.Annexe_Record record = new Jy123.Model.Annexe_Record();
                                record.FAnnexe = listAnnexe[0].ID;
                                record.FModule = vId;
                                record.IsUsed = true;
                                BLLARecord.Add(record);
                            }
                        }
                        strRedirect = "/Module/Venues/MyVenues.aspx?VenuesId=" + moduleId.ToString();
                        break;
                    case 2:
                        Public_SportType BLLSportType = new Public_SportType();
                        Jy123.Model.Public_SportType sportType = BLLSportType.GetModel(JType.FSportType.Value);
                        Club_Main BLLClub = new Club_Main();
                        Jy123.Model.Club_Main club = new Jy123.Model.Club_Main();
                        club.ID = moduleId;
                        club.CName = name;
                        club.Description = description;
                        club.FJType = JType.ID;
                        club.FLevel = level.ID;
                        club.FUser = userId;
                        club.Assets = 0;
                        club.Experience = 0;
                        club.WinTimes = 0;
                        club.LoseTimes = 0;
                        club.CStatus = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.ClubStatus.Empty);
                        club.RaceTimes = 0;
                        club.Rate = 0;
                        club.Form = 1;
                        club.Experience = 0;
                        club.LiveNess = 0;
                        club.PhysicalFitness = 0;
                        club.Skill = 0;
                        club.Morale = 0;
                        BLLClub.Add(club);
                        int quantity = sportType.Quantity.Value;
                        Club_StarRecord BLLRecord = new Club_StarRecord();

                        if (sportType.ID == 13 || sportType.ID == 14)
                        {
                            BLL.Public_Cars BLLCars = new Public_Cars();
                            List<Model.Public_Cars> listCars = BLLCars.GetModelList("FSportType = " + sportType.ID.ToString());
                            if (listCars.Count > 0)
                            {
                                BLL.Club_Cars BLLClubCars = new Club_Cars();
                                Model.Club_Cars clubCars = new Jy123.Model.Club_Cars();
                                clubCars.FClub = moduleId;
                                clubCars.IsUsed = true;
                                clubCars.FClubCarsType = listCars[0].ID;
                                BLLClubCars.Add(clubCars);
                            }
                            strRedirect = "/Module/CarClub/MyClub.aspx?ClubId=" + moduleId.ToString();
                        }
                        else
                        {
                            for (int i = 0; i < quantity; i++)
                            {
                                Jy123.Model.Club_StarRecord record = new Jy123.Model.Club_StarRecord();
                                record.FClub = moduleId;
                                record.SType = 3;
                                BLLRecord.Add(record);
                            }
                            strRedirect = "/Module/Club/MyClub.aspx?ClubId=" + moduleId.ToString();
                        }
                        break;
                    case 3:
                        TCenter_Main BLLTCenter = new TCenter_Main();
                        Jy123.Model.TCenter_Main TCenter = new Jy123.Model.TCenter_Main();
                        if (listConsume.Count > 0)
                        {
                            Model.Public_Consume consume = listConsume[0];
                            peakValue = consume.PeakValue.Value;
                        }
                        TCenter.ID = moduleId;
                        TCenter.TName = name;
                        TCenter.FLevel = level.ID;
                        TCenter.FUser = userId;
                        TCenter.Vitality = peakValue;
                        TCenter.Capacity = level.Scope.Value;
                        TCenter.CurrentClubs = 0;
                        TCenter.Assets = 0;
                        TCenter.UsingTimes = 0;
                        BLLTCenter.Add(TCenter);
                        strRedirect = "/Module/TCenter/MyTCenter.aspx?TCenterId=" + moduleId.ToString();
                        break;
                }
                return strRedirect;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 状态改变
        /// </summary>
        /// <param name="id">组件ID</param>
        /// <param name="status">状态ID</param>
        /// <returns>成功失败</returns>
        public bool StatusType(int id, int vStatus)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("UPDATE Module_Main SET MStatus = {0} WHERE ID = {1}", vStatus, id);
            if (DbHelperSQL.ExecuteSql(strSql.ToString()) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 判断用户是否拥有最大组件数
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="maxCount">初始定义的用户最大组件数</param>
        /// <returns>是或者否</returns>
        public bool HaveMaxModule(int userId, int maxCount)
        {
            Module_Main BLLModule = new Module_Main();
            string strWhere = string.Format("FUser = {0}", userId);
            List<Jy123.Model.Module_Main> listModule = BLLModule.GetModelList(strWhere.ToString());
            if (listModule.Count >= maxCount)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取用户没有的所有一级组件
        /// </summary>
        /// <param name="listModule">用户拥有的组件</param>
        /// <returns>用户没有的所有一级组件定义列表</returns>
        public List<Jy123.Model.Public_Level> GetOtherModule(List<Jy123.Model.Module_Main> listModule)
        {
            Public_Level BLLLevel = new Public_Level();
            string strWhere = "LevelNumber = '1'";
            List<Jy123.Model.Public_Level> allLevel = BLLLevel.GetModelList(strWhere);
            List<Jy123.Model.Public_Level> listResult = new List<Jy123.Model.Public_Level>();
            foreach (Jy123.Model.Public_Level level in allLevel)
            {
                if (!IsInList(listModule, level.FJType.Value))
                {
                    listResult.Add(level);
                }
            }
            return listResult;
        }
        /// <summary>
        /// 获取用户拥有的所有一级组件
        /// </summary>
        /// <param name="listModule">用户拥有的组件</param>
        /// <returns>用户拥有的所有一级组件定义列表</returns>
        public List<Jy123.Model.Public_Level> GetAllUserModule(List<Jy123.Model.Module_Main> listModule)
        {
            Public_Level BLLLevel = new Public_Level();
            string strWhere = "LevelNumber = '1'";
            List<Jy123.Model.Public_Level> allLevel = BLLLevel.GetModelList(strWhere);
            List<Jy123.Model.Public_Level> listResult = new List<Jy123.Model.Public_Level>();
            foreach (Jy123.Model.Public_Level level in allLevel)
            {
                if (!IsInList(listModule, level.FJType.Value))
                {
                    listResult.Add(level);
                }
            }
            return listResult;
        }
        /// <summary>
        /// 获取所有一级组件,用户没有的方前面，用户拥有的放后面
        /// </summary>
        /// <param name="listModule">用户拥有的组件</param>
        /// <returns>排序后所有一级组件定义列表</returns>
        public List<Jy123.Model.Public_Level> GetAllLevel(List<Jy123.Model.Module_Main> listModule)
        {
            Public_Level BLLLevel = new Public_Level();
            string strWhere = "LevelNumber = '1'";
            List<Jy123.Model.Public_Level> allLevel = BLLLevel.GetModelList(strWhere);
            List<Jy123.Model.Public_Level> listResult = new List<Jy123.Model.Public_Level>();
            foreach (Jy123.Model.Public_Level level in allLevel)
            {
                if (!IsInList(listModule, level.FJType.Value))
                {
                    listResult.Add(level);
                }
            }
            foreach (Jy123.Model.Public_Level level in allLevel)
            {
                if (IsInList(listModule, level.FJType.Value))
                {
                    listResult.Add(level);
                }
            }
            return listResult;
        }
        /// <summary>
        /// 判断用户是否拥有该组件定义类型的组件
        /// </summary>
        /// <param name="listModule">用户的所有组件</param>
        /// <param name="JType">目前组件定义的大类型</param>
        /// <returns>是或者否</returns>
        public bool IsInList(List<Jy123.Model.Module_Main> listModule, int JType)
        {
            for (int i = 0; i < listModule.Count; i++)
            {
                if (JType == listModule[i].FJType.Value)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 根据用户和组件类型获取组件
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="FJType">组件类型</param>
        /// <returns>组件列表</returns>
        public List<Jy123.Model.Module_Main> GetModuleList(int userId, int FJType)
        {
            Module_Main BLLModuleMain = new Module_Main();
            string strWhere = string.Format("FUser = {0} AND FJType = {1}", userId, FJType);
            List<Jy123.Model.Module_Main> moduleList = BLLModuleMain.GetModelList(strWhere);
            if (moduleList.Count > 0)
            {
                return moduleList;
            }
            return null;
        }
        /// <summary>
        /// 组件升级(金钱)
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="money">升级所需金钱</param>
        /// <returns>成功失败</returns>
        public bool LevelUpByMoney(int id, decimal money)
        {
            Jy123.BLL.User_Main BLLUserMain = new Jy123.BLL.User_Main();
            Jy123.Model.User_Main user = BLLUserMain.GetModel(id);
            if (user.Cash.Value >= money)
            {
                return true;
            }
            return false;
        }

        public List<Jy123.Model.Module_Main> FriendsModule(int userId, int JType)
        {
            Module_Main BLLModuleMain = new Module_Main();
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendFormat("FJType = {0} ", JType);
            strWhere.Append("AND FUser IN (");
            strWhere.Append("SELECT FFriend FROM User_Friend ");
            strWhere.AppendFormat("WHERE FUser = {0})", userId);
            List<Jy123.Model.Module_Main> resultList = BLLModuleMain.GetModelList(strWhere.ToString());
            return resultList;
        }
        /// <summary>
        /// 获取用户的现金和所有组件的总资产之和
        /// </summary>
        /// <param name="user">用户</param>
        /// <returns>用户的现金和所有组件的总资产之和</returns>
        public int GetUserAllMoney(Model.User_Main user)
        {
            BLL.Venues_Main BLLVenues = new Venues_Main();
            BLL.Club_Main BLLClub = new Club_Main();
            BLL.TCenter_Main BLLTCenter = new TCenter_Main();
            decimal assets = 0;
            string strWhere = "FUser = " + user.ID.ToString();
            List<Model.Venues_Main> listVenues = BLLVenues.GetModelList(strWhere);
            List<Model.Club_Main> listClub = BLLClub.GetModelList(strWhere);
            List<Model.TCenter_Main> listTCenter = BLLTCenter.GetModelList(strWhere);
            foreach (Model.Venues_Main venues in listVenues)
            {
                if (venues.Assets.HasValue)
                {
                    assets += venues.Assets.Value;
                }
            }
            foreach (Model.Club_Main club in listClub)
            {
                if (club.Assets.HasValue)
                {
                    assets += club.Assets.Value;
                }
            }
            foreach (Model.TCenter_Main TCenter in listTCenter)
            {
                if (TCenter.Assets.HasValue)
                {
                    assets += TCenter.Assets.Value;
                }
            }
            assets += user.Cash.Value;
            return Convert.ToInt32(assets);
        }
    }
}
